import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// 本地存储服务
/// await Get.putAsync<StorageService>(() => StorageService().init());
/// StorageService.to.setString('key', 'value')
class StorageService extends GetxService {
  static StorageService get to => Get.find();

  late SharedPreferences _sp;

  Future<StorageService> init() async {
    _sp = await SharedPreferences.getInstance();
    return this;
  }

  /// 设置
  Future<bool> setBool(String key, bool value) async {
    return await _sp.setBool(key, value);
  }

  Future<bool> setInt(String key, int value) async {
    return await _sp.setInt(key, value);
  }

  Future<bool> setDouble(String key, double value) async {
    return await _sp.setDouble(key, value);
  }

  Future<bool> setString(String key, String value) async {
    return await _sp.setString(key, value);
  }

  Future<bool> setStringList(String key, List<String> value) async {
    return await _sp.setStringList(key, value);
  }

  /// 获取
  bool? getBool(String key) => _sp.getBool(key);

  int? getInt(String key) => _sp.getInt(key);

  double? getDouble(String key) => _sp.getDouble(key);

  String? getString(String key) => _sp.getString(key);

  List<String>? getStringList(String key) => _sp.getStringList(key);

  /// 移除
  Future<bool> remove(String key) async {
    return await _sp.remove(key);
  }
}
